package com.alipay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.jfinal.kit.JsonKit;
import com.jfinal.log.Log;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝的SDK
 *
 * @author 肖得明
 */
public class AlipaySDK {
	public static String appid = "2019110869021343";
	public static String private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDHgE65krBjSBa+Zj9e0IXIFpBWpmDY7Qr1a4EYV9jz5wkOfAP6GRexNSUs5YYKldPVsF29mItR4xxYynMYU3TqetkPEOieeyjzR/QNKFyUdXSN/yJTM2TU1e+BIGBEU/s/lQWTFN817GqN0YxW+zboGIW86Xo2FHEO9TblsZwUsbyn5/DSdO72h1uzXLKwF3M637CUUE7U539YVp3pkExJELW1m8Q1eHRixTZebTbL2oShzq8en7NCLYqrqYfG5PYaf8HhSS94G8YxGib/O9HcJ9bw4CMAG8GLasVBakCXD6+HH1XK9HsWDBQB244ZQEOYWM/T3WGAmlTl2q1dCfk/AgMBAAECggEAKi6d9qzmcMeQT4fNBF4wu0LSGBAr0BImzAeED0Kb085Y4uAj8yCVGtny11BcjCZr2PXmjC0R4CmWF8wOk+kpgl6hQIdBuCiwWugu2Q1creM71r5IYrEuM7ZcYsAWurvLFwGKYTP7kio/yWayNr39OQveUzhGp+KgmRoOFnj8WH9v7oCy+K17Kv4b0bpGYuKHHMtLsKfSnWvtY6sh5Tx2bzq5DZ686Fy2O6D+RzCyYSqCqCMV2E3IyOjvzwFsX6ZWoFlgXpnBj/OR+DnyNL9kEAhvOXWny97EZxWE5iJtnXaMWot/Qr9bV1iA07+Wi+TsiQhvKTwmm4X5/UHgtjnuuQKBgQD/PWfpD0C6oqq/ogAmI8hdX7DWToOyoT+eGdwnN1YemnYsLF/7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mttD6bsfvbDSu1Fwjp5MHL1X+Zh1XVHi+UhcUp8a8MsQrBVMJ6E52oRaIrmysvZcLzFS+imrqJtsH8ooNJumway1hLo4kwZp4xOKLBWdJ5iWTqFE5BXMbcI0ky+JJu27qo2+NH8t3yGKN6hg/KG9ruItLNCI7MgPMbzPGHEkzQIDAQAB";
	private static Log log = Log.getLog(AlipaySDK.class);

	// 商户的私钥
	/*
	 * public static String appid="2088312327296955"; public static String private_key = "doe186hl3n4kjue61ydcbq1gpxsv8e8m"; // 支付宝的公钥，无需修改该值 public static String alipay_public_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
	 */
	public static void main(String[] args) throws AlipayApiException {
		AlipayTradeRefundResponse resp = refund("12334422115776884070061577688410956", "0.01");
		System.err.println(JsonKit.toJson(resp));
		/*AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appid, private_key, "json", "GBK", alipay_public_key, "RSA2");
		AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
		Map<String, String> map = new HashMap<String, String>();
		map.put("out_trade_no", "93491528536763773");
		request.setBizContent(JsonKit.toJson(map));
		AlipayTradeQueryResponse response = alipayClient.execute(request);
		if (response.isSuccess()) {
			if(response.getCode().equals("10000")){
				System.err.println(response.getTotalAmount());
			}
			
			System.out.println("调用成功"+response.getBody());
		} else {
			System.out.println("调用失败");
		}*/
		/*// out_trade_no:1232221554556478811，trade_no:2017073021001004160288018689
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appid, private_key, "json", "GBK", alipay_public_key, "RSA2");
		AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
		Map<String, String> map = new HashMap<String, String>();
		map.put("out_trade_no", "12322215545564788111"); // 订单支付时传入的商户订单号,不能和 trade_no同时为空。
		map.put("trade_no", "2017073021001004160288018689");// 支付宝交易号，和商户订单号不能同时为空
		map.put("refund_amount", "0.01");// 需要退款的金额，该金额不能大于订单金额,单位为元，支持两位小数
		map.put("refund_reason", "正常退款");// 退款的原因说明
		map.put("operator_id", "AppSystem");// 商户的操作员编号
		map.put("store_id", "t0004");// 商户的门店编号
		map.put("terminal_id", "001");// 商户的终端编号
		request.setBizContent(JSONObject.toJSONString(map));
		AlipayTradeRefundResponse response = alipayClient.execute(request);
		System.err.println(response.getBody());
		if (response.isSuccess()) {
			System.out.println(response.getMsg());
		} else {
			System.out.println(response.getMsg());
		}*/
	}

	public static AlipayTradeRefundResponse refund(String orderid, String money) {
		money = BigDecimal.valueOf(Double.valueOf(money)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appid, private_key, "json", "GBK", alipay_public_key, "RSA2");
		AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
		Map<String, String> map = new HashMap<String, String>();
		map.put("out_trade_no", orderid); // 订单支付时传入的商户订单号,不能和 trade_no同时为空。
		// map.put("trade_no", "2017073021001004160288018689");//支付宝交易号，和商户订单号不能同时为空
		map.put("refund_amount", money);// 需要退款的金额，该金额不能大于订单金额,单位为元，支持两位小数
		map.put("refund_reason", "正常退款");// 退款的原因说明
		map.put("operator_id", "AppSystem");// 商户的操作员编号
		map.put("store_id", "t0004");// 商户的门店编号
		map.put("terminal_id", "001");// 商户的终端编号
		request.setBizContent(JSONObject.toJSONString(map));
		try {
			AlipayTradeRefundResponse response = alipayClient.execute(request);
			return response;
		} catch (AlipayApiException e) {
			log.error("退款失败：", e);
			return null;
		}
	}
}
